package ru.ipo.dces.problemsviewer;

import ru.ipo.dces.pluginapi.PluginEnvironment;

import javax.swing.*;
import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * User: ilya
 * Date: 09.06.11
 * Time: 22:00
 */
public class TabComponent extends JPanel {

    private final JComponent pluginPanel;
    private final HistoryTable historyTable;
    private final JPanel historyPanel = new JPanel();

    private final PluginEnvironment environment;

    private boolean historyVisible = false;

    public TabComponent(JComponent pluginPanel, ProblemSubmissionHistory history, PluginEnvironment environment) {
        this.pluginPanel = pluginPanel;
        this.environment = environment;

        historyTable = new HistoryTable(history);
        initHistoryPanel();

        setLayout(new BorderLayout());

        layoutComponents();
    }

    private void initHistoryPanel() {
        historyPanel.setLayout(new BorderLayout());
        historyPanel.add(new JScrollPane(historyTable), BorderLayout.CENTER);
        //TODO align to center
        historyPanel.add(new JLabel("<html><h1>&nbsp;История</h1></html>"), BorderLayout.NORTH);
    }

    public boolean isHistoryVisible() {
        return historyVisible;
    }

    public void setHistoryVisible(boolean historyVisible) {
        this.historyVisible = historyVisible;
        layoutComponents();
    }

    private void layoutComponents() {
        removeAll();

        if (historyVisible) {
            JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, pluginPanel, historyPanel);
            splitPane.setResizeWeight(1);
            add(splitPane, BorderLayout.CENTER);
        } else
            add(pluginPanel, BorderLayout.CENTER);

        validate();
        repaint();
    }

    public PluginEnvironment getEnvironment() {
        return environment;
    }
}
